/**
 ******************************* ICE *******************************
 * ========================================================================
 * Project:		iceservices
 * Package:		org.ice4apps.service
 * File:		IceOrganizationService.java
 * Author: 		robin
 * Date: 		May 5, 2010
 * Description:	
 * ========================================================================
 * 
 */

package org.ice4apps.service;

import java.util.List;
import java.util.UUID;
import javax.ejb.Stateless;
import org.ice4apps.service.bean.LoginServiceRequest;
import org.ice4apps.service.bean.OfficeAddServiceRequest;
import org.ice4apps.service.bean.OfficeGetServiceResponse;
import org.ice4apps.service.bean.OfficeListEntry;
import org.ice4apps.service.bean.OfficeListGetServiceResponse;
import org.ice4apps.master.dao.Office;
import org.ice4apps.master.dao.Organization;
import org.ice4apps.master.dataservices.IceMasterDataServiceLocator;
import org.ice4apps.talk.dao.TalkOffice;
import org.ice4apps.talk.dao.TalkOrganization;
import org.ice4apps.talk.dataservices.IceTalkDataServiceLocator;

@Stateless
public class IceOrganizationService extends IceOfficeService implements IceOrganizationServiceLocal {

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceOrganizationServiceLocal#addOffice(java.lang.String, org.ice4apps.service.bean.OfficeAddServiceRequest)
     */
    public void addOffice (String sessionId, OfficeAddServiceRequest req) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().getSession (sessionId);
            req.validate ();
            String id = UUID.randomUUID ().toString ();
            Office office = new Office ();
            office.setId (id);
            office.setFeatures (Long.MAX_VALUE);
            office.setName (req.getName ());
            Organization org = IceMasterDataServiceLocator.getIceMasterDataService ().getOrganizationByName (req.getOrganizationName ());
            office.setOrganization (org);
            Long creationDate = System.currentTimeMillis ();
            office.setCreationdate (creationDate);
            IceMasterDataServiceLocator.getIceMasterDataService ().addOffice (office);
            TalkOffice talkOffice = new TalkOffice ();
            talkOffice.setId (id);
            talkOffice.setCreationdate (creationDate);
            talkOffice.setFeatures (Long.MAX_VALUE);
            TalkOrganization talkOrg = IceTalkDataServiceLocator.getIceTalkDataService ().getOrganization (org.getId ());
            talkOffice.setTalkOrganization (talkOrg);
            talkOffice.setDialPrefix (req.getPrefix ());
            IceTalkDataServiceLocator.getIceTalkDataService ().addOffice (talkOffice);
        } catch (Exception ex1) {
        }
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceOrganizationServiceLocal#getOffice(java.lang.String, java.lang.String)
     */
    public OfficeGetServiceResponse getOffice (String sessionId, String name) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().getSession (sessionId);
            Office office = IceMasterDataServiceLocator.getIceMasterDataService ().getOfficeByName (name);
            TalkOffice talkOffice = IceTalkDataServiceLocator.getIceTalkDataService ().getOffice (office.getId ());
            OfficeGetServiceResponse resp = new OfficeGetServiceResponse ();
            resp.setEnterpriseName (office.getOrganization ().getEnterprise ().getName ());
            resp.setOrganizationName (office.getOrganization ().getName ());
            resp.setName (office.getName ());
            resp.setPrefix (talkOffice.getDialPrefix ());
            return resp;
            
        } catch (Exception ex1) {
        }
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceOrganizationServiceLocal#getOfficeList(java.lang.String, java.lang.String)
     */
    public OfficeListGetServiceResponse getOfficeList (String sessionId,
                                                       String orgName) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().getSession (sessionId);
            List<Office> offices = IceMasterDataServiceLocator.getIceMasterDataService ().getOfficeListByOrganization (orgName);
            OfficeListGetServiceResponse resp = new OfficeListGetServiceResponse();
            for (Office office : offices) {
                OfficeListEntry entry = new OfficeListEntry ();
                entry.setEnterpriseName (office.getOrganization ().getEnterprise ().getName ());
                entry.setOrganizationName (office.getOrganization ().getName ());
                entry.setName (office.getName ());
                resp.add (entry);
            }
            return resp;
            
        } catch (Exception ex1) {
        }
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceOrganizationServiceLocal#login(org.ice4apps.service.bean.LoginServiceRequest)
     */
    public String login (LoginServiceRequest req) {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceOrganizationServiceLocal#logout(java.lang.String)
     */
    public void logout (String sessionId) {
        // TODO Auto-generated method stub
        
    }
}
